package com.kaesar.leetcode.LeetCode_251_300;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LeetCode_290 {
    public static boolean wordPattern(String pattern, String s) {
        String[] strs = s.split(" ");
        List<String> strList = new ArrayList<>();
        for (String str : strs) {
            if (str != "") {
                strList.add(str);
            }
        }
        // 判断strList的数量和pattern的长度是否相同，如果不相同，说明无法遵循规律，直接返回false
        if (pattern.length() != strList.size()) {
            return false;
        }
        Map<Character, String> mappings = new HashMap<>();
        for (int i = 0; i < strList.size(); i++) {
            if (mappings.keySet().contains(pattern.charAt(i))) {
                if (!strList.get(i).equals(mappings.get(pattern.charAt(i)))) {
                    return false;
                }
            } else {
                if (mappings.values().contains(strList.get(i))) {
                    return false;
                }
                mappings.put(pattern.charAt(i), strList.get(i));
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(wordPattern("abba", "dog dog dog dog"));
    }
}
